﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json;
using Common;
using TCWireless.Component.Utility;
using TCWireless.Component.Utility.Security;
namespace ToolS
{
    public partial class Form1 : Form
    {
        private PathEntity CurrentPathEntity = null;
        public Form1()
        {
            InitializeComponent();

            Init.InitWorkDirectory();

            //RefreshUrl();

            cb_requestMethod.SelectedIndex = 0;

            LoadRequests();

        }

        private void LoadRequests()
        {
            WorkSettings ws = RequestEntityHelper.GetWorkSetting();
            if (ws != null && ws.PathEntities != null)
            {
                ws.PathEntities.ForEach(item =>
                {
                    this.RequestsList.DisplayMember = "SavedName";
                    this.RequestsList.ValueMember = "ParentPath";
                    this.RequestsList.Items.Add(item);
                });
            }
        }

        private void json格式化_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text = ConvertJsonString(this.richTextBox1.Text);
        }
        private string ConvertJsonString(string str)
        {

            //格式化json字符串
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string url = this.comboBox1.Text;
            if (!LoadUrls.AddUrl(url))
            {
                MessageBox.Show("已存在");
            }
            RefreshUrl();
        }

        private void RefreshUrl()
        {
            var result = LoadUrls.LoadUrlsFromFile();
            if (result != null && result.Any())
            {
                this.comboBox1.Items.Clear();
                result.ForEach(item =>
                {
                    this.comboBox1.Items.Add(item.url);
                });
            }
        }

        private void RefreshUrl(List<string> urls)
        {

            if (urls != null && urls.Any())
            {
                this.comboBox1.Items.Clear();
                urls.ForEach(item =>
                {
                    this.comboBox1.Items.Add(item);
                });
            }
        }

        private void RefreshRequestEntity(List<string> requests)
        {
            if (requests != null && requests.Any())
            {
                this.requestentity.Items.Clear();
                requests.ForEach(item =>
                {
                    this.requestentity.Items.Add(item.Take(40));
                });
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ReqData reqData = JsonConvert.DeserializeObject<ReqData>(this.rb_request.Text);
            RequestCommon.NomalizeRequest(reqData);
            string reqJson = JsonConvert.SerializeObject(reqData).Replace("\r", "").Replace("\n", "");

            HttpWebRequestMethod method = "GET".Equals(this.cb_requestMethod.SelectedText, StringComparison.OrdinalIgnoreCase) ? HttpWebRequestMethod.GET : HttpWebRequestMethod.POST;

            string strResponse = RequestCommon.PostDataToServer(this.comboBox1.Text, reqJson, method, "4957CA66-37C3-46CB-B26D-E3D9DCB51535", "4");

            strResponse = ConvertJsonString(strResponse);

            this.rb_response.Text = strResponse;
        }

        private void RequestsList_SelectedValueChanged(object sender, EventArgs e)
        {
            PathEntity nv = this.RequestsList.SelectedValue as PathEntity;

            if (!nv.SavedName.Equals(CurrentPathEntity.ParentPath, StringComparison.OrdinalIgnoreCase))
            {
                var result = RequestEntityHelper.LoadRequestEntity(nv);
                InitNewRequest(result);
            }
        }

        private void InitNewRequest(RequestEntity newRequestEntity)
        {
            RefreshUrl(newRequestEntity.RequestUrlList);
            RefreshRequestEntity(newRequestEntity.RqeustContent);
            this.cb_requestMethod.Text = newRequestEntity.HttpWebRequestMethod == HttpWebRequestMethod.GET ? "GET" : "POST";
        }
    }
}
